package com.dz.lm.web.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.util.ScanCodeUtil;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.service.goldagent.GoldAgentService;
import com.dz.lm.web.controller.BasicControl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import java.io.IOException;
import java.util.Map;
import java.util.Vector;

@RestController
@RequestMapping(value = "/rewriteUrl/goldAgent", produces = "application/json")
public class GoldAgentController extends BasicControl {

	public Logger logger = LogManager.getLogger(GoldAgentController.class);
	
    @Autowired
    private GoldAgentService goldAgentService;
    
    /**
     * 获取黄金代理人列表
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryGoldAgentList")
    public Map<String, Object> qryGoldAgentList(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = null;
    	//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("position");
		inparam.add("size");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
				subjo = new JSONObject();
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		String position = param.get("position") != null? param.get("position").toString() : "0";
		String size = param.get("size") != null? param.get("size").toString() : "10";		
		param.put("position", Integer.valueOf(position) * Integer.valueOf(size));
		param.put("size", Integer.valueOf(size));
		Map<String, Object> retData = goldAgentService.qryGoldAgentList(param);
		subjo = new JSONObject();
		subjo.put("result", "00");
    	subjo.put("message", "SUCCESS");
    	subjo.put("agentList", retData.get("agentList"));
		int count = (int) retData.get("count");
		int page = 0;
		if (count % Integer.valueOf(size) > 0) {
			page = count / Integer.valueOf(size) + 1;
		} else {
			page = count / Integer.valueOf(size);
		}
    	subjo.put("count", count);
    	subjo.put("page", page);
		return parameterEncrypt(request, subjo);
    }

    /**
     * 查看我的推广码
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryMyPromoCode")
    public Map<String, Object> qryMyPromoCode(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = null;
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		//用户编码
		param.put("userId", request.getAttribute("userId"));
		Map<String, Object> user = goldAgentService.qryMyPromoCode(param);
		if (user != null && !user.isEmpty()) {
			subjo = new JSONObject();
			subjo.put("result", "00");
	    	subjo.put("message", "查询成功");
	    	subjo.put("promoCode", user.get("promoCode")== null ? "" : user.get("promoCode"));
		} else {
			subjo = new JSONObject();
	    	subjo.put("result", "01");
	    	subjo.put("message", "未查的推广码");
	    	subjo.put("promoCode", null);
		}
		return parameterEncrypt(request, subjo);
    }

    /**
     * 申请成为黄金代理人
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/applyGoldAgent")
    public Map<String, Object> applyGoldAgent(HttpServletRequest request) throws ServiceException {
    	Object imgUrl = request.getAttribute("imgUrl");
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();;
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		//用户编码
		param.put("userId", request.getAttribute("userId"));
		try {
			Map<String, Object> retCodeInfo = ScanCodeUtil.getWxScanCode(request.getAttribute("userId").toString(), ScanCodeUtil.postToken(), imgUrl);
			param.put("scanCode", imgUrl + retCodeInfo.get("scanCode").toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		Map<String, Object> retData = goldAgentService.applyGoldAgent(param);
		Object result = retData.get("result");
		if ("0".equals(result)) {
			subjo.put("result", "00");
	    	subjo.put("message", "恭喜您开通成功");
		} else if ("2".equals(result)) {
			subjo.put("result", "01");
	    	subjo.put("message", "您还未完成任务，无法开通");
		} else if ("3".equals(result)) {
			subjo.put("result", "02");
	    	subjo.put("message", "开通黄金一级代理需取消所有二级代理产品的代理权限，请先到代理产品列表取消二级代理");
		}
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 查看（生成）个人专属店铺链接
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryPerStorInfo")
    public Map<String, Object> qryPerStorInfo(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		//用户编码
		if (param.get("userId") == null || "".equals(param.get("userId"))) {
			param.put("userId", request.getAttribute("userId"));
		}
		Map<String, Object> perStorInfo = goldAgentService.qryPerStorInfo(param);
		if (perStorInfo != null && !perStorInfo.isEmpty()) {
			subjo.put("result", "00");
	    	subjo.put("message", "查询成功");
	    	subjo.put("perOpenStoreFlag", perStorInfo.get("perOpenStoreFlag")== null ? "" : perStorInfo.get("perOpenStoreFlag"));
	    	subjo.put("perStoreUrl", perStorInfo.get("perStoreUrl")== null ? "" : perStorInfo.get("perStoreUrl"));
	    	subjo.put("scanCode", perStorInfo.get("scanCode")== null ? "" : perStorInfo.get("scanCode"));
		} else {
	    	subjo.put("result", "01");
	    	subjo.put("message", "未查的个人专属店铺链接");
	    	subjo.put("perOpenStoreFlag", "0");
	    	subjo.put("perStoreUrl", null);
	    	subjo.put("scanCode", null);
		}
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 申请个人专属店铺
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/applyPerStore")
    public Map<String, Object> applyPerStore(HttpServletRequest request) throws ServiceException {
    	Object imgUrl = request.getAttribute("imgUrl");
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();;
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		//用户编码
		param.put("userId", request.getAttribute("userId"));
		try {
			Map<String, Object> retCodeInfo = ScanCodeUtil.getWxScanCode(request.getAttribute("userId").toString(), ScanCodeUtil.postToken(), imgUrl);
			param.put("scanCode", imgUrl + retCodeInfo.get("scanCode").toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		Map<String, Object> retData = goldAgentService.applyPerStore(param);
		Object perStoreUrl = retData.get("perStoreUrl");
		if (perStoreUrl != null && !"".equals(perStoreUrl)) {
			subjo.put("result", "00");
	    	subjo.put("message", "恭喜您个人店铺开通成功");
	    	subjo.put("perStoreUrl", perStoreUrl);
	    	subjo.put("scanCode", param.get("scanCode"));
		} else {
			subjo.put("result", "01");
	    	subjo.put("message", "个人店铺开通失败");
	    	subjo.put("perStoreUrl", null);
	    	subjo.put("scanCode", null);
		}
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 查询个人主页申请记录列表
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryMyAllProductApplyList")
    public Map<String, Object> qryMyAllProductApplyList(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = null;
    	//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("position");
		inparam.add("size");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
				subjo = new JSONObject();
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		String position = param.get("position") != null? param.get("position").toString() : "0";
		String size = param.get("size") != null? param.get("size").toString() : "10";		
		param.put("position", Integer.valueOf(position) * Integer.valueOf(size));
		param.put("size", Integer.valueOf(size));
		if (param.get("userId") == null || "".equals(param.get("userId"))) {
			param.put("userId", request.getAttribute("userId"));
		}
		Map<String, Object> retData = goldAgentService.qryMyAllProductApplyList(param);
		subjo = new JSONObject();
		subjo.put("result", "00");
    	subjo.put("message", "SUCCESS");
    	subjo.put("applyList", retData.get("applyList"));
    	subjo.put("historyApplyCount", retData.get("historyApplyCount"));
    	subjo.put("totalPayAmount", retData.get("totalPayAmount"));
		int count = (int) retData.get("count");
		int page = 0;
		if (count % Integer.valueOf(size) > 0) {
			page = count / Integer.valueOf(size) + 1;
		} else {
			page = count / Integer.valueOf(size);
		}
    	subjo.put("count", count);
    	subjo.put("page", page);
		return parameterEncrypt(request, subjo);
    }
}
